import { ElMessage } from "element-plus";
import { ActionTree } from "vuex";
import { Iobj, Iquestionoption, asyncselectarticle, asyncselectlabel, asyncselectpackagekey, asyncselectquestion, asyncselectquestionbank, asyncselectspecialjudge, asyncselectteams, asyncselectuser, asyncselectuservip, online } from '../../api';
import { Istate } from "../../interface";
import Study from '../../mysql_interface/studycodemodel';
import * as types from './mutation-types';

const actions: ActionTree<Istate, Istate> = {
  async actiononline({ commit, state }) {
    try {
      const data = await online()
      // 需要判断是否为老师
      if (!data.isAdmin) {
        ElMessage.error('权限不足')
        return false
      }
      if (!data.teamid) {
        // ElMessage.error('团队校验失败')
        console.error("actiononline data.teamid is", data.teamid)
        return false
      }
      const n = await asyncselectuservip({ userid: data.ID })
      data.uservip = n.rows
      commit(types.SET_U, data)
      state.indexsocket?.emit('inteamid', { userid: data.ID, teamid: data.teamid })
      return true
    } catch (e: any) {
      console.log(e)
      return false
    }
  },
  async actionOnlineuser({ commit, state }) {
    state.indexsocket?.on('inteamid', (onlineUser: { userid: number, teamid: number }[]) => {
      commit(types.SET_ONLINEUSER, onlineUser)
    })
  },

  async actionusers({ commit }, body?: Iobj<Study.Iuser>) {
    try {
      const data = await asyncselectuser(body)
      commit(types.SET_USERS, data)
    } catch (e: any) {
      commit(types.SET_USERS, [])
    }
  },
  // async actionactivity({ commit }, body: Iobj<Study.Iactivity> = {}) {
  //   try {

  //     const data = await asyncadminselectactivity(body)
  //     commit(types.SET_ACTIVITY, data)
  //   } catch (e: any) {
  //     commit(types.SET_ACTIVITY, [])
  //   }
  // },
  async actionarticle({ commit }, body?: Iobj<Study.Iarticle>) {
    try {
      const data = await asyncselectarticle(body)
      commit(types.SET_ARTICLE, data)
    } catch (e: any) {
      commit(types.SET_ARTICLE, [])
    }
  },
  async actionquestion({ commit }, body: Iobj<Iquestionoption> = {}) {
    if (!body._sortable) {
      body._sortable = 'ASC'
    }
    try {
      const data = await asyncselectquestion(body)
      // const data = await asyncadminselectquestion(body)
      commit(types.SET_QUESTION, data)
    } catch (e: any) {
      commit(types.SET_QUESTION, [])
    }
  },
  async actionquestionbank({ commit }, body?: Iobj<Study.Iquestionbank>) {
    try {
      const data = await asyncselectquestionbank(body)
      // const data = await asyncadminselectquestionbank(body)
      commit(types.SET_QUESTIONBANK, data)
    } catch (e: any) {
      commit(types.SET_QUESTIONBANK, [])
    }
  },
  async actionlabel({ commit }, body?: Iobj<Study.ILabel>) {
    try {
      const data = await asyncselectlabel()
      commit(types.SET_LABEL, data)
    } catch (e: any) {
      commit(types.SET_LABEL, [])
    }
  },
  // team TODO
  async actionteam({ commit }, body: any = {}) {
    try {
      const data = await asyncselectteams(body)
      commit(types.SET_TEAM, data)
    } catch (e: any) {
      commit(types.SET_TEAM, [])
    }
  },
  async actionpackegekey({ commit }, body: any = {}) {
    try {
      const data = await asyncselectpackagekey(body)
      commit(types.SET_PACKEGEKEY, data)
    } catch (e) {
      commit(types.SET_PACKEGEKEY, [])
      return e
    }
  },
  async actionspecialjudge({ commit }, body: any = {}) {
    try {
      const data = await asyncselectspecialjudge(body)
      commit(types.SET_SPECIALJUDGE, data)
    } catch (e) {
      commit(types.SET_SPECIALJUDGE, [])
      return e
    }
  }
}


export default actions